package com.sxsy.datasource.util;

import com.sxsy.datasource.bean.SwjcBean;
import com.sxsy.datasource.biz.SwjcData;
import com.sxsy.datasource.bizimpl.SwjcDataImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@Repository
public class SwjcFileUtil {
    @Autowired
    private SwjcData swjcData;
    @Autowired
    private SwjcBean swjcBean ;
    public int Keyindex = 2;
    public int Wzindex = 3;
    public int Dwindex = 8;
    public int Valueindex = 5;
    public int Dateindex = 6;
    int size;
    public static Map<String, String[]> Devmap = new HashMap<String, String[]>() ;
    public void RtfileAnalysis() throws IOException {
        File file = new File("D:\\KJ1049\\EXE\\share\\SWRTDATA.txt");
        BufferedReader br = new BufferedReader(new FileReader("D:\\KJ1049\\EXE\\share\\SWRTDATA.txt"));//构造一个BufferedReader类来读取文件
        String s = null;
       // Map<String, String> map = new HashMap<String, String>();
        int i = 0;
        while((s = br.readLine())!=null){//使用readLine方法，一次读一行
            if(i>=0){
                String[] str = s.split("\\,");
                if(Devmap.containsKey(str[Keyindex])){

                    //map.put("VALUE",str[Valueindex]);
                    //map.put("Date",str[Dateindex]);
                    String[] tmp = Devmap.get(str[Keyindex]);
                    if(tmp[Keyindex] == "61010100000001" ){
                        swjcData.RtsaveNewSsKeyValue(str[Keyindex],str[Valueindex],str[8]);
                        swjcData.HistoryTableinsert(tmp[Keyindex],tmp[Wzindex],tmp[Dwindex],str[Valueindex],str[8]);
                    }
                    //map.put("BM",tmp[Keyindex]);
                   // map.put("WZ",tmp[Wzindex]);
                    //map.put("DW",tmp[Dwindex]);
                    swjcData.HistoryTableinsert(tmp[Keyindex],tmp[Wzindex],tmp[Dwindex],str[Valueindex],str[Dateindex]);
                    swjcData.RtsaveNewSsKeyValue(str[Keyindex],str[Valueindex],str[Dateindex]);

                }


            }
//            System.out.println("编码		"+map.get("BM"));
//            System.out.println("位置		"+map.get("WZ"));
//            System.out.println("单位		"+map.get("DW"));
//            System.out.println("实际值	"+map.get("VALUE"));
//            System.out.println("时间		"+map.get("Date"));
            i++;

        }

        br.close();
        //file.delete();
    }
    public  void DevfileAnalysis() throws IOException{
        File file = new File("D:\\KJ1049\\EXE\\share\\SWDEV.txt");
        BufferedReader br = new BufferedReader(new FileReader("D:\\KJ1049\\EXE\\share\\SWDEV.txt"));//构造一个BufferedReader类来读取文件
        String s = null;
        String[] oldstr = null;
        String[] newstr = null;
        //定义两个map；设备map；
        //设备属性存储
        Map<String, String[]> NewDevmap = new HashMap<String, String[]>();
        String key = null;
        int i = 0;
        while((s = br.readLine())!=null){//使用readLine方法，一次读一行
            if(i>=0){
                String[] str = s.split("\\,");
                key = str[Keyindex];
                NewDevmap.put(key,str);
                size = str.length;

            }

            i++;

        }

        br.close();
        //file.delete();
        List<SwjcBean> swjcdata = swjcData.DoesKeyExit();
        if (swjcdata.isEmpty()) {

            for (Entry<String, String[]> Newentry : NewDevmap.entrySet()) {
                if (Devmap.containsKey(Newentry.getKey()) != true) {
                    Devmap.put(Newentry.getKey(), NewDevmap.get(Newentry.getKey()));
                    String addkey = Newentry.getKey();
                    String wz = Devmap.get(Newentry.getKey())[Wzindex];
                    String dw = Devmap.get(Newentry.getKey())[Dwindex];
                    //数据库增加方法（key，WZ，DW）
                    List<String> KeyList = new ArrayList<>();
                    swjcData.RtsaveNewDevkeyValue(addkey, wz, dw);

                }
            }
        }else{
            if(Devmap.size()==0){
                for(int a = 0 ; a< swjcdata.size();a++){
                    String[] devstr = new String[size];
                    SwjcBean bean = swjcdata.get(a);
                    //System.out.println(swjcdata.get(a).toString());
                    devstr[Keyindex] = bean.getCjgjk_Swjc_Rt_Bm();
                    devstr[Wzindex] = bean.getCjgjk_Swjc_Rt_Wz();
                    devstr[Dwindex] =bean.getCjgjk_Swjc_Rt_Dw();
                    devstr[Valueindex] = bean.getCjgjk_Swjc_Rt_Value();
                    devstr[Dateindex] = bean.getCjgjk_Swjc_Rt_Date();
                    Devmap.put(devstr[Keyindex],devstr);

                }
            }
            //正向比较
            for(Entry<String, String[]> Newentry : NewDevmap.entrySet()) {
                if(Devmap.containsKey(Newentry.getKey())!=true) {
                    Devmap.put(Newentry.getKey(), NewDevmap.get(Newentry.getKey()));
                    String addkey = Newentry.getKey();
                    String wz = Devmap.get(Newentry.getKey())[Wzindex];
                    String dw = Devmap.get(Newentry.getKey())[Dwindex];
                    //数据库增加方法（key，WZ，DW）
                    swjcData.RtsaveNewDevkeyValue(addkey,wz,dw);
                }

            }
            //反向比较
            List<String> rmKeylist = new ArrayList<>();
            for(Entry<String, String[]> Oldentry : Devmap.entrySet()) {

                if(NewDevmap.containsKey(Oldentry.getKey())!=true) {
                   // Devmap.remove(Oldentry.getKey());
                    String rmkey = Oldentry.getKey();
                    rmKeylist.add(rmkey);
                    //数据库减方法（where key  = 。。。）
                    swjcData.RtdeleteRow(rmkey);
                }



            }
            for(int z = 0; z<rmKeylist.size();z++){
                Devmap.remove(rmKeylist.get(z));
            }
            for(Entry<String, String[]> Oldentry : Devmap.entrySet()) {
                newstr = NewDevmap.get(Oldentry.getKey());
                oldstr = Devmap.get(Oldentry.getKey());
                if (newstr[Wzindex] != oldstr[Wzindex]) {
                    swjcData.RtupdateWz(Oldentry.getKey(), newstr[Wzindex]);
                    oldstr[Wzindex] = newstr[Wzindex];
                }
                if (newstr[Dwindex] != oldstr[Dwindex]) {
                    swjcData.RtupdateDw(Oldentry.getKey(), newstr[Dwindex]);
                    oldstr[Dwindex] = newstr[Dwindex];

                }
                Devmap.put(Oldentry.getKey(), oldstr);
            }


        }



        RtfileAnalysis();
    }

}
